package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.VO.shop.ShopAppletSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopAppletVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.shop.service.IBackShopAppletService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺-小程序管理
 */
@Controller
@RequestMapping("/back/shopApplet")
public class BackShopAppletController extends BaseController {
    private String prefix = "shop/applet";

    @Autowired
    private IBackShopAppletService appletService;

    /**
     * 页面-店铺小程序列表
     *
     * @return
     */
    @RequiresPermissions("back:shopApplet:list")
    @GetMapping
    public String list() {
        return prefix + "/list";
    }

    /**
     * 页面-新增店铺小程序
     *
     * @return
     */
    @RequiresPermissions("back:shopApplet:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 页面-编辑店铺小程序
     *
     * @param shopAppletId 店铺ID
     * @return
     */
    @RequiresPermissions("back:shopApplet:view")
    @GetMapping("/edit/{shopAppletId}")
    public String edit(@PathVariable("shopAppletId") Long shopAppletId, ModelMap mmap) {
        mmap.put("shopApplet", appletService.getShopAppletInfoById(shopAppletId));
        return prefix + "/edit";
    }

    /**
     * 页面-店铺-编辑店铺小程序信息
     *
     * @return
     */
    @RequiresPermissions("back:shopApplet:info")
    @GetMapping("/info")
    public String info(ModelMap mmap) {
        if(0 == ShiroUtils.getShopId()){
            return "redirect:/back/shop/switchShop";
        }
        mmap.put("shopApplet", appletService.getShopAppletInfoByShopId(ShiroUtils.getShopId()));
        return prefix + "/info";
    }

    /**
     * 数据查询-店铺小程序列表（搜索）
     *
     * @param shopSearchVO
     * @return
     */
    @RequiresPermissions("back:shopApplet:list")
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(ShopAppletSearchVO shopSearchVO) {
        startPage();
        List<ShopAppletVO> list = appletService.searchList(shopSearchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-校验公司名称
     */
    @PostMapping("/checkCompanyNameUnique")
    @ResponseBody
    public String checkShopNameUnique(ShopAppletSearchVO shopAppletSearchVO) {
        return appletService.checkShopAppletNameUnique(shopAppletSearchVO);
    }

    /**
     * 数据新增-添加店铺小程序
     *
     * @param shopAppletVO
     * @return
     */
    @Log(title = "总后台-店铺小程序管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:shopApplet:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(ShopAppletVO shopAppletVO) {
        ShopAppletSearchVO shopAppletSearchVO = new ShopAppletSearchVO();
        shopAppletSearchVO.setAppletName(shopAppletVO.getAppletName());
        if (Constants.FAIL.equals(appletService.checkShopAppletNameUnique(shopAppletSearchVO))) {
            return error("新增店铺小程序'" + shopAppletVO.getAppletName() + "'失败，店铺小程序已存在");
        }
        shopAppletVO.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        return toAjax(appletService.insertShopApplet(shopAppletVO));
    }

    /**
     * 数据更新-编辑店铺小程序信息
     *
     * @param shopAppletVO 店铺小程序信息
     * @return
     */
    @Log(title = "总后台-店铺小程序管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions(value = {"back:shopApplet:edit", "back:shopApplet:editMyself"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopAppletVO shopAppletVO) {
        ShopAppletSearchVO shopAppletSearchVO = new ShopAppletSearchVO();
        shopAppletSearchVO.setShopAppletId(shopAppletVO.getId());
        shopAppletSearchVO.setAppletName(shopAppletVO.getAppletName());
        if (Constants.FAIL.equals(appletService.checkShopAppletNameUnique(shopAppletSearchVO))) {
            return error("修改店铺小程序'" + shopAppletSearchVO.getAppletName() + "'失败，店铺小程序已存在不允许覆盖");
        }
        shopAppletVO.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        return AjaxResult.success(appletService.updateShopApplet(shopAppletVO));
    }

    /**
     * 数据更新-删除店铺小程序
     *
     * @param ids
     * @return
     */
    @Log(title = "总后台-店铺小程序管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:shopApplet:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult delete(String ids) {
        Long shopId = Long.valueOf(ids);
        return toAjax(appletService.deleteShopAppletById(shopId));
    }
}
